package com.rmdjzz.slmmn.controller.user.recommend;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.rmdjzz.slmmn.pojo.base.Song;
import com.rmdjzz.slmmn.pojo.result.Result;
import com.rmdjzz.slmmn.pojo.vo.PageVo;
import com.rmdjzz.slmmn.pojo.vo.SongVo;
import com.rmdjzz.slmmn.service.SingerService;
import com.rmdjzz.slmmn.service.SongService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.ArrayList;
import java.util.List;

/**
 * @作者 张绍鑫
 * @创建时间 2023/6/7
 */
@Controller
@RequestMapping("/user")
@Api(tags = "推荐管理api文档")
@Slf4j
public class RecommendController {

    @Autowired
    private SongService songService;
    @Autowired
    private SingerService singerService;
    @GetMapping("/recommend")
    @ResponseBody
    @ApiOperation("推荐")
    public Result recommend(PageVo pageVo){

        Page<Song> page = new Page<>(pageVo.getStartIndex(), pageVo.getSize());
        //根据id查询
        QueryWrapper<Song> songQueryWrapper = new QueryWrapper<>();
        if (pageVo.getStr().equals("芒种")){
            songQueryWrapper.eq("recommend_id",1);
        }else if (pageVo.getStr().equals("霜降")){
            songQueryWrapper.eq("recommend_id",2);
        }else if (pageVo.getStr().equals("谷雨")){
            songQueryWrapper.eq("recommend_id",3);
        }else if (pageVo.getStr().equals("小雪")){
            songQueryWrapper.eq("recommend_id",4);
        }else if (pageVo.getStr().equals("冬至")){
            songQueryWrapper.eq("recommend_id",5);
        }else if (pageVo.getStr().equals("大暑")){
            songQueryWrapper.eq("recommend_id",6);
        }else if (pageVo.getStr().equals("夏至")){
            songQueryWrapper.eq("recommend_id",7);
        }else if (pageVo.getStr().equals("立春")){
            songQueryWrapper.eq("recommend_id",8);
        }
        //查询到数据
        Page songPage = songService.page(page,songQueryWrapper);
        //将数据转为vo
        List<Song> records = songPage.getRecords();
        ArrayList<SongVo> songVos = new ArrayList<>();
        for (Song song:records){
            log.info("song:"+song);
            SongVo songVo = new SongVo();
            songVo.setSongName(song.getSongName());
            if (singerService.getById(song.getSingerId())==null){
                songVo.setSingerName("未知歌手");
            }else {
                songVo.setSingerName(singerService.getById(song.getSingerId()).getSingerName());
            }
            songVos.add(songVo);
        }
        songPage.setRecords(songVos);
        return Result.ok(songPage);
    }

    @GetMapping("/playRecommend/{recommend}")
    @ApiOperation("榜单播放")
    public String playRankList(@PathVariable String recommend, Model model){

        //根据播放次数排行
        QueryWrapper<Song> songQueryWrapper = new QueryWrapper<>();
        if (recommend.equals("芒种")){
            songQueryWrapper.eq("recommend_id",1);
        }else if (recommend.equals("霜降")){
            songQueryWrapper.eq("recommend_id",2);
        }else if (recommend.equals("谷雨")){
            songQueryWrapper.eq("recommend_id",3);
        }else if (recommend.equals("小雪")){
            songQueryWrapper.eq("recommend_id",4);
        }else if (recommend.equals("冬至")){
            songQueryWrapper.eq("recommend_id",5);
        }else if (recommend.equals("大暑")){
            songQueryWrapper.eq("recommend_id",6);
        }else if (recommend.equals("夏至")){
            songQueryWrapper.eq("recommend_id",7);
        }else if (recommend.equals("立春")){
            songQueryWrapper.eq("recommend_id",8);
        }
        List<Song> records = songService.list(songQueryWrapper);
        ArrayList<SongVo> songVos = new ArrayList<>();
        ArrayList<String> words = new ArrayList<>();
        for (Song song:records){
            SongVo songVo = new SongVo();
            songVo.setId(song.getId());
            songVo.setSongImg(song.getSongImg());

            songVo.setWords(song.getWords());
            songVo.setSongName(song.getSongName());
            songVo.setSongTime(song.getSongTime());
            if (singerService.getById(song.getSingerId())==null){
                songVo.setSingerName("未知歌手");
            }else {
                songVo.setSingerName(singerService.getById(song.getSingerId()).getSingerName());
            }
            songVo.setSong(song.getSong());
            log.info("song："+song);
            log.info("songVo："+songVo);
            words.add(song.getWords());
            songVos.add(songVo);
        }
        model.addAttribute("list",songVos);
        model.addAttribute("words",words);
        model.addAttribute("msg",recommend);
        return "play";
    }
}
